//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "IndexedCashFlow.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Index.h>
#include <gen/QL/CashFlow.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (Double notional, Cephei::QL::IIndex^ index, DateTime baseDate, DateTime fixingDate, DateTime paymentDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ growthOnly) : CCashFlow(CIndexedCashFlow::typeid)
{
    CIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phIndexedCashFlow = NULL;
#endif
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional);
        _Cindex = safe_cast<CIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::Index>& _index = static_cast<boost::shared_ptr<QuantLib::Index>&> (_Cindex->GetShared ()); 
        QuantLib::Date _baseDate = (QuantLib::Date)ValueHelper::Convert (baseDate);
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate);
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate);
        bool _growthOnly = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (growthOnly) ? (bool)ValueHelper::Convert (growthOnly->Value) : false); //4
        _ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (new QuantLib::IndexedCashFlow ( _notional,  _index,  _baseDate,  _fixingDate,  _paymentDate,  _growthOnly ));
        SetCashFlow (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppIndexedCashFlow));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (boost::shared_ptr<QuantLib::IndexedCashFlow>& childNative, Object^ owner) : CCashFlow(CIndexedCashFlow::typeid)
{
#ifdef HANDLE
	_phIndexedCashFlow = NULL;
#endif
	_ppIndexedCashFlow = &childNative;
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppIndexedCashFlow));
}
Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (QuantLib::IndexedCashFlow& childNative, Object^ owner) : CCashFlow(CIndexedCashFlow::typeid)
{
#ifdef HANDLE
	_phIndexedCashFlow = NULL;
#endif
	_ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (&childNative);
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppIndexedCashFlow));
    _IndexedCashFlowOwner = owner;
    _CashFlowOwner = owner;
}

Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (CIndexedCashFlow^ copy) : CCashFlow(CIndexedCashFlow::typeid)
{
#ifdef HANDLE
	_phIndexedCashFlow = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (copy->GetShared());
        _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppIndexedCashFlow));
    }
}
Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (System::Type^ t) : CCashFlow(CIndexedCashFlow::typeid)
{
#ifdef HANDLE
	_phIndexedCashFlow = NULL;
#endif
	if (!t->IsSubclassOf(CIndexedCashFlow::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (QuantLib::Handle<QuantLib::IndexedCashFlow>& childNative, Object^ owner)  : CCashFlow(CIndexedCashFlow::typeid)
{
	_phIndexedCashFlow = &childNative;
	_ppIndexedCashFlow = &static_cast<boost::shared_ptr<QuantLib::IndexedCashFlow>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppIndexedCashFlow));
    _IndexedCashFlowOwner = owner;
}
Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (QuantLib::Handle<QuantLib::IndexedCashFlow> childNative)  : CCashFlow(CIndexedCashFlow::typeid)
{
	_phIndexedCashFlow = &childNative;
	_ppIndexedCashFlow = &static_cast<boost::shared_ptr<QuantLib::IndexedCashFlow>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppIndexedCashFlow));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CIndexedCashFlow::CIndexedCashFlow (QuantLib::IndexedCashFlow childNative)  : CCashFlow(CIndexedCashFlow::typeid)
{
#ifdef HANDLE
	_phIndexedCashFlow = NULL;
#endif
	_ppIndexedCashFlow = new boost::shared_ptr<QuantLib::IndexedCashFlow> (new QuantLib::IndexedCashFlow (childNative));
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppIndexedCashFlow));
}
#endif

Cephei::QL::Cashflows::CIndexedCashFlow::~CIndexedCashFlow ()
{
    if (_ppIndexedCashFlow != NULL)
    {
	    delete _ppIndexedCashFlow;
        _ppIndexedCashFlow = NULL;
    }
}
Cephei::QL::Cashflows::CIndexedCashFlow::!CIndexedCashFlow ()
{
    if (_ppIndexedCashFlow != NULL)
    {
	    delete _ppIndexedCashFlow;
    }
}
QuantLib::IndexedCashFlow& Cephei::QL::Cashflows::CIndexedCashFlow::GetReference ()
{
    if (_ppIndexedCashFlow == NULL) throw gcnew NativeNullException ();
	return **_ppIndexedCashFlow;
}
boost::shared_ptr<QuantLib::IndexedCashFlow>& Cephei::QL::Cashflows::CIndexedCashFlow::GetShared ()
{
    if (_ppIndexedCashFlow == NULL) throw gcnew NativeNullException ();
	return *_ppIndexedCashFlow;
}
QuantLib::IndexedCashFlow* Cephei::QL::Cashflows::CIndexedCashFlow::GetPointer ()
{
    if (_ppIndexedCashFlow == NULL) throw gcnew NativeNullException ();
	return &**_ppIndexedCashFlow;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IndexedCashFlow>& Cephei::QL::Cashflows::CIndexedCashFlow::GetHandle ()
{
	if (_phIndexedCashFlow == NULL)
	{
		_phIndexedCashFlow = new Handle<QuantLib::IndexedCashFlow> (*_ppIndexedCashFlow);
	}
	return *_phIndexedCashFlow;
}
#endif
bool Cephei::QL::Cashflows::CIndexedCashFlow::HasNative () 
{
	return (_ppIndexedCashFlow != NULL);
}

Double Cephei::QL::Cashflows::CIndexedCashFlow::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIndexedCashFlow)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CIndexedCashFlow::BaseDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppIndexedCashFlow)->baseDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CIndexedCashFlow::Date::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppIndexedCashFlow)->date ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CIndexedCashFlow::FixingDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppIndexedCashFlow)->fixingDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IIndex^ Cephei::QL::Cashflows::CIndexedCashFlow::Index::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Index> _rv = (boost::shared_ptr<QuantLib::Index>)(*_ppIndexedCashFlow)->index ( );   
        Cephei::QL::CIndex^ _nrv = gcnew Cephei::QL::CIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CIndexedCashFlow::Notional::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIndexedCashFlow)->notional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Cashflows::CIndexedCashFlow::GrowthOnly::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppIndexedCashFlow)->growthOnly ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IIndexedCashFlow^ Cephei::QL::Cashflows::CIndexedCashFlow::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppIndexedCashFlow)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IIndexedCashFlow^ Cephei::QL::Cashflows::CIndexedCashFlow_Factory::Create (Double notional, Cephei::QL::IIndex^ index, DateTime baseDate, DateTime fixingDate, DateTime paymentDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ growthOnly)
{
    return gcnew CIndexedCashFlow ( notional,  index,  baseDate,  fixingDate,  paymentDate,  growthOnly);
}
